import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);
import * as api from "../api";
import allRoutes from "../router/allRoutes";
import dynamicRoutes from "../router/dynamicRoutes";
import router from "../router";
import recursion from "../utils/recursionRouter";
let userInfo = localStorage.getItem("qf-userInfo") || "{}";
export default new Vuex.Store({
  state: {
    userInfo: JSON.parse(userInfo),
    sideMenu: [], //侧边栏数据
    bread: [], //面包屑数据
    dialogVisible: false,
    title: "增加学员信息",
    tableData: [],
    loading: true,
    //修改的时候，dialog表格回显的数据
    nowRow: {},
    stuTotal: 0,
    stuPageSize: 10,
  },
  mutations: {
    //修改dialog回显时的数据
    SET_NOWROW(state, payload) {
      state.nowRow = { ...payload };
      console.log(state.nowRow);
    },
    SHOW_LOADING(state) {
      state.loading = true;
    },
    HIDE_LOADING(state) {
      state.loading = false;
    },
    //
    SET_TABLEDATA(state, payload) {
      state.tableData = payload.data; //设置学员列表
      state.loading = false;
      state.stuTotal = payload.total; //设置学员分页总数
    },
    SHOW_DIALOG(state) {
      state.dialogVisible = true;
    },
    HIDE_DIALOG(state) {
      state.dialogVisible = false;
    },
    SET_TITLE(state, payload) {
      state.title = payload;
    },
    SET_BREAD(state, payload) {
      state.bread = payload;
    },
    SET_USER_INFO(state, payload) {
      state.userInfo = payload;
      // let target =dynamicRoutes.find(item=>item.path==="/")
      // console.log(target)
      // target.children=payload
      // router.addRoute(dynamicRoutes)
    },
    SET_MENULIST(state, payload) {
      state.sideMenu = payload;

      let target = dynamicRoutes.find((item) => item.path === "/");

      target.children = payload;
      // console.log(dynamicRoutes)
      dynamicRoutes.forEach((routes) => {
        router.addRoute(routes);
      });
    },
  },

  actions: {
    async GET_MENULIST({ commit }) {
      // console.log("action触发了");
      let res = await api.getMenuListApi();
      let { menuList } = res.data;
      // console.log(menuList)
      let userRoutes = recursion(menuList, allRoutes);
      commit("SET_MENULIST", userRoutes);
    },
    GET_TABLEDATA({ commit }, payload = {}) {
      commit("SHOW_LOADING");
      let { page = 1, count = 10, classes = "" } = payload;
      api.getStuListApi(page, count, classes).then((res) => {
        commit("SET_TABLEDATA", res.data);
      });
    },
  },
  modules: {},
});
